#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    qp_Wipe = new QProcess;

    //#############
    qs_Wipe = "wipe";
    //#############

    b_RealyWipe = false;

    qgl_Grid = new QGridLayout;

    this->CreateButtons();
    this->CreateRadioButtons();

    qgl_Grid->addWidget(qrb_FileRadioButton,0,0);
    qgl_Grid->addWidget(qrb_FolderRadioButton,1,0);

    qgl_Grid->addWidget(qpb_OpenButton,0,1);
    qgl_Grid->addWidget(qpb_CloseButton,1,1);

    this->setLayout(qgl_Grid);
    this->ShowAll();
}

Dialog::~Dialog()
{

}

void Dialog::SelectFile()
{
    if(qrb_FileRadioButton->isChecked())
    {
      qfd_FileDialog.setFileMode( QFileDialog::ExistingFile );

      if(qfd_FileDialog.exec() == QFileDialog::Accepted)
      {
            CreateMessageBox();
      }

      if(b_RealyWipe == true)
      {
          qsl_List = qfd_FileDialog.selectedFiles();
          for (int i = 0; i < qsl_List.size(); i++)
            {
                qs_File = qsl_List[i];

                //#############
                QStringList test;
                test << "-r";
                test << "-f";
                test << qsl_List[i];
                qDebug() << test;
                qp_Wipe->start(qs_Wipe,test);

                connect(qp_Wipe,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(Done()));
                //#############
             }
        }

    }
    if(qrb_FolderRadioButton->isChecked())
    {
      qfd_FileDialog.setFileMode( QFileDialog::Directory );

      if(qfd_FileDialog.exec() == QFileDialog::Accepted)
      {
            CreateMessageBox();
      }

      if(b_RealyWipe == true)
      {
          qsl_List = qfd_FileDialog.selectedFiles();
          for (int i = 0; i < qsl_List.size(); i++)
            {

                qsl_List = qfd_FileDialog.selectedFiles();
                for (int i = 0; i < qsl_List.size(); i++)
                  {
                      qs_File = qsl_List[i];

                      //#############
                      QStringList test;
                      test << "-r";
                      test << "-f";
                      test << qsl_List[i];
                      qDebug() << test;
                      qp_Wipe->start(qs_Wipe,test);

                      connect(qp_Wipe,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(Done()));
                      //#############
            }
        }

    }
}
}

void Dialog::Quit()
{
    close();
}

void Dialog::ShowAll()
{
    QObject::connect(qpb_OpenButton, SIGNAL(clicked()),this, SLOT(SelectFile()));
    QObject::connect(qpb_CloseButton, SIGNAL(clicked()),this, SLOT(Quit()));
    qpb_OpenButton->show();
    qpb_CloseButton->show();
}

void Dialog::CreateButtons()
{
    qpb_OpenButton = new QPushButton("Open");
    qpb_CloseButton = new QPushButton("Close");
    qpb_OpenButton->setMinimumWidth(80);
    qpb_OpenButton->setMaximumWidth(80);
    qpb_CloseButton->setMinimumWidth(80);
    qpb_CloseButton->setMaximumWidth(80);
}

void Dialog::CreateRadioButtons()
{
    qrb_FileRadioButton = new QRadioButton(tr("Wipe a File"));
    qrb_FolderRadioButton = new QRadioButton(tr("Wipe a Folder"));
}

void Dialog::CreateMessageBox()
{
    qmb_MsgBoxDelete = new QMessageBox;
    qmb_MsgBoxDelete->setText("You realy want to WipE this?");
    qmb_MsgBoxDelete->addButton(tr("WipE"), QMessageBox::AcceptRole);
    qmb_MsgBoxDelete->addButton(tr("Quit"), QMessageBox::RejectRole);

    int r = qmb_MsgBoxDelete->exec();

    switch(r)
    {
        case QMessageBox::AcceptRole:
        b_RealyWipe = true;
        break;

        case QMessageBox::RejectRole:
        b_RealyWipe = false;
        break;
    }

}

void Dialog::Done()
{
    qDebug() << "Well Done!";
}
